package com.eastfair.common.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.eastfair.common.dto.JYMainnoResultDTO;
import com.eastfair.common.req.JYReq;
import com.eastfair.common.res.JYFinanceRes;
import com.eastfair.common.service.CommonInterfaceCallRecordService;
import com.eastfair.common.service.FinanceService;
import com.eastfair.core.exception.BizException;
import io.seata.common.util.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 荆艺财务同步
 * </p>
 *
 * @author clm
 * @date 2022-06-09
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class FinanceServiceImpl implements FinanceService {
    @Value("${finance.jy_url}")
    private String jyUrl;

    @Resource
    private CommonInterfaceCallRecordService commonInterfaceCallRecordService;


    @Override
    public List<JYMainnoResultDTO> sync(JYReq req) {
        String res = commonInterfaceCallRecordService.processHttp(jyUrl, req);
        JYFinanceRes<JYMainnoResultDTO> result = JSONObject.parseObject(res, new TypeReference<JYFinanceRes<JYMainnoResultDTO>>() {
        });
        if (result != null && result.isSuccess() && CollectionUtils.isNotEmpty(result.getData())) {
            log.info("同步财务系统成功：入参={},返回值={}", req, result);
            return result.getData();
        }
        log.info("同步财务系统失败：入参={},返回值={}", req, result);
        throw new BizException("同步财务系统失败");
    }

}
